package com.jin.spirng_boot_learn;


import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

public class RunForever {


    public Thread thread;

    public void runSpecialMethod() {
        System.out.println("111");

        thread = new Thread(()->{
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("runing");
            }
        });
        thread.setDaemon(true);
    }


//    @PreDestroy
//    public void shutdown() {
//        System.out.println("77777777777777777777777777777777777777");
//        thread.interrupt();
//    }

    @Slf4j
    @Component
    public static class TaskService {


        @Async("taskExecutor")
        public void doTaskOne() throws Exception {
            TaskService.log.info("开始做任务一");
            long start = System.currentTimeMillis();
            Thread.sleep(1000*1);
            long end = System.currentTimeMillis();
            TaskService.log.info("完成任务一，耗时：" + (end - start) + "毫秒");
        }

        @Async("taskExecutor")
        public void doTaskTwo() throws Exception {
            TaskService.log.info("开始做任务二");
            long start = System.currentTimeMillis();
            Thread.sleep(1000*2);
            long end = System.currentTimeMillis();
            TaskService.log.info("完成任务二，耗时：" + (end - start) + "毫秒");
        }

        @Async("taskExecutor")
        public void doTaskThree() throws Exception {
            TaskService.log.info("开始做任务三");
            long start = System.currentTimeMillis();
            Thread.sleep(1000*3);
            long end = System.currentTimeMillis();
            TaskService.log.info("完成任务三，耗时：" + (end - start) + "毫秒");
        }

    }
}
